<template>
  <div class="container">
    <el-card>

      <el-main>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="学科名称">
            <el-input v-model="requestParameters1.subjectName" placeholder="学科名称" />
          </el-form-item>

          <el-form-item>
            <el-button @click="empty">清除</el-button>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>

          <el-button class="prompt" type="text" @click="dialogVisible = true"><i class="el-icon-edit" />新增学科</el-button>

          <el-dialog title="新增学科" :visible.sync="dialogVisible" width="400px" :before-close="handleClose">

            <el-form>
              <el-form-item label="学科名称" :rules="[{ required: true}]">
                <el-input v-model="courseName" autocomplete="off" style="width:260px" placeholder="请输入学科名称" />
              </el-form-item>
              <el-form-item>
                <span> 是否显示</span>
                <el-switch v-model="showTag" active-color="#13ce66" inactive-color="#ff4949" />
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false;courseName=''">取 消</el-button>
              <el-button type="primary" @click="commitCourse">确 定</el-button>
            </span>
          </el-dialog>

        </el-form>

        <el-alert :title="alertText" type="info" show-icon :closable="false" />

        <!-- 数据列表 -->
        <el-table :key="tableKey" v-loading="listLoading" :data="subList" element-loading-text="给我一点时间" fit highlight-current-row style="width: 100%">
          <el-table-column type="index" align="center" label="序号">
            <!-- <template slot-scope="scope">
              <span>{{scope.row.id}}</span>
            </template> -->
          </el-table-column>

          <el-table-column align="center" label="学科名称">
            <template slot-scope="scope">
              <span>{{ scope.row.subjectName }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="创建者">
            <template slot-scope="scope">
              <span>{{ scope.row.username }}</span>
            </template>
          </el-table-column>

          <el-table-column label="创建日期">
            <template slot-scope="scope">
              <span>{{ subTime(scope.row.addDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="前台是否显示">
            <template slot-scope="scope">
              <span>{{ scope.row.isFrontDisplay }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="二级目录">
            <template slot-scope="scope">
              <span>{{ scope.row.twoLevelDirectory }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="标签">
            <template slot-scope="scope">
              <span>{{ scope.row.tags }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center" label="题目数量">
            <template slot-scope="scope">
              <span>{{ scope.row.totals }}</span>
            </template>
          </el-table-column>
          <!-- 操作按钮 -->
          <el-table-column align="center" label="操作" width="280" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button type="text" @click="toSub(scope.row.id,scope.row.subjectName)">学科分类</el-button>

              <el-button type="text" @click="SubLabels(scope.row.id,scope.row.subjectName)">学科标签</el-button>

              <!-- 学科修改 -->
              <el-button type="text" @click="onModifyClick(scope.row)">修改</el-button>
              <el-dialog title="学科修改" :visible.sync="dialogVisible1" width="400px" :before-close="handleClose1">

                <el-form>
                  <el-input v-model="courseId" style="display:none" />
                  <el-form-item label="学科名称" :rules="[{ required: true}]">
                    <el-input v-model="courseName" autocomplete="off" style="width:260px" placeholder="请输入学科名称" />
                  </el-form-item>
                  <el-form-item>
                    <span> 是否显示</span>
                    <el-switch v-model="showTag" active-color="#13ce66" inactive-color="#ff4949" />
                  </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible1 = false;courseName=''">取 消</el-button>
                  <el-button type="primary" @click="commitRevise()">确 定</el-button>
                </span>
              </el-dialog>

              <el-button v-if="scope.row.status!='deleted'" size="mini" type="danger" @click="removeSub(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <el-pagination background :current-page="requestParameters1.page" :page-sizes="[5, 10, 20, 30]" :page-size="requestParameters1.pagesize" layout="sizes, prev, pager, next, jumper, ->, total, slot" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />

      </el-main>
    </el-card>
  </div>
</template>

<script>
import moment from 'moment'
import { list, add, update, remove } from '@/api/hmmm/subjects'
export default {
  name: 'SubjectList',
  data () {
    return {
      total: 0,
      tableKey: 2,
      courseId: 0,
      // 学科列表
      subList: [],
      listLoading: true,
      courseName: '',
      showTag: true,
      formInline: {
        user: ''
      },
      dialogVisible: false,
      dialogVisible1: false,
      requestParameters1: {
        page: 1,
        pagesize: 10,
        subjectName: ''

      },
      alertText: ''

    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 跳转到标签页
    SubLabels (a, b) {
      this.$router.push(`/subjects/tags??id=${a}&name=${b}`)
    },

    // 跳转到目录页面
    toSub (a, b) {
      this.$router.push(`/subjects/directorys?id=${a}&name=${b}`)
    },
    // 每页几条
    handleSizeChange (val) {
      this.requestParameters1.pagesize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.requestParameters1.page = val
      this.getList()
    },

    // 删除
    removeSub (user) {
      this.$confirm('此操作将永久删除学科 ' + ', 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: user })
            .then(response => {
              this.$message.success('成功删除了用户' + '!')
              // this.subList.splice(user, 1)
              this.getList()
            })
            .catch(response => {
              this.$message.error('删除失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },

    // 操作栏修改点击事件
    onModifyClick (item) {
      console.log(item)
      console.log(item.subjectName)
      this.courseId = item.id
      this.courseName = item.subjectName
      this.showTag = item.isFrontDisplay === 1
      this.dialogVisible1 = true
    },
    // 学科修改
    commitRevise () {
      this.dialogVisible1 = false
      update({ id: this.courseId, subjectName: this.courseName, isFrontDisplay: this.showTag }).then(data => {
        if (data.status === 200) {
          this.getList()
        }
      })
      this.courseName = ''
    },

    // 格式化时间
    subTime (row) {
      return moment(row).format('YYYY-MM-DD HH:mm:ss')
    },

    // 初始数据
    initialDate () {
      // 读取数据
      this.getList()
    },
    // 获取列表数据
    async getList (params) {
      console.log(this.requestParameters1)
      this.listLoading = true
      const { data: res } = await list({ page: this.requestParameters1.page, pagesize: this.requestParameters1.pagesize, subjectName: this.requestParameters1.subjectName })
      console.log(res)
      // 获取数据给dataList
      this.subList = res.items
      // 获取总条数
      this.total = res.counts
      this.alertText = `共 ${this.total} 条记录`
      this.listLoading = false
    },

    // 学科查询
    async onSubmit () {
      const ret = await list(this.requestParameters1)
      console.log(ret)
      this.subList = ret.data.items
      // this.requestParameters1.subjectName = ''
    },

    // 学科清除
    empty () {
      this.requestParameters1.subjectName = ''
    },
    handleClose () {
      this.dialogVisible = false
      this.courseName = ''
    },
    handleClose1 () {
      this.dialogVisible1 = false
      this.courseName = ''
    },

    // 学科提交
    commitCourse () {
      this.dialogVisible = false
      add({ subjectName: this.courseName, isFrontDisplay: this.showTag }).then(data => {
        if (data.status === 200) {
          this.getList()
          this.$message.success('成功添加了用户' + '!')
        }
      })
      this.courseName = ''
    }
  }
}

</script>

<style scoped lang='sass'>
.prompt
  width: 90px
  height: 35px
  float: right
  color: #fff
  background-color: #67C23A
</style>
